package cn.getech.data.development.utils;

import cn.getech.data.development.constant.ConnectTypeEnum;
import cn.getech.data.development.entity.dataMap.HiveDataType;
import cn.getech.data.development.mapper.dataMap.HiveDataTypeMapper;
import cn.getech.data.development.utils.spring.SpringContextUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Set;

/**
 * @description: 数据类型装换
 * @author: wangzhaowen：kiss
 * @create: 2020/12/30 17:28
 * @version: 2020/12/30
 **/
public class DataTypeTransfer {

    private static HiveDataTypeMapper hiveDataTypeMapper = SpringContextUtil.getBean(HiveDataTypeMapper.class);

    public static Set<String> getDataType(Integer type) {
        Set<String> types = Sets.newHashSet();
        List<HiveDataType> list = hiveDataTypeMapper.selectList(new QueryWrapper<>());
        if (ConnectTypeEnum.MySQL.getCode().equals(type)) {
            for (HiveDataType hiveDataType : list) {
                if (StringUtils.isNotEmpty(hiveDataType.getMysqlType())) {
                    types.add(hiveDataType.getMysqlType());
                }
            }
        } else if (ConnectTypeEnum.Oracle.getCode().equals(type)) {
            for (HiveDataType hiveDataType : list) {
                if (StringUtils.isNotEmpty(hiveDataType.getMysqlType())) {
                    types.add(hiveDataType.getOracleType());
                }
            }
        } else if (ConnectTypeEnum.SQL_Server.getCode().equals(type)) {
            for (HiveDataType hiveDataType : list) {
                if (StringUtils.isNotEmpty(hiveDataType.getMysqlType())) {
                    types.add(hiveDataType.getOracleType());
                }
            }
        } else if (ConnectTypeEnum.SAPHANA.getCode().equals(type)) {
            for (HiveDataType hiveDataType : list) {
                if (StringUtils.isNotEmpty(hiveDataType.getMysqlType())) {
                    types.add(hiveDataType.getOracleType());
                }
            }
        } else if (ConnectTypeEnum.PostgreSQL.getCode().equals(type)) {
            for (HiveDataType hiveDataType : list) {
                if (StringUtils.isNotEmpty(hiveDataType.getMysqlType())) {
                    types.add(hiveDataType.getOracleType());
                }
            }
        }
        return types;
    }


    /**
     * 数据库类型
     *
     * @param type #see cn.getech.data.development.constant.ConnectTypeEnum
     * @return
     */
    public static String transferDataType(String oriType, Integer type) {
        QueryWrapper<HiveDataType> param = new QueryWrapper<>();
        String column = "mysql_type";
        if (ConnectTypeEnum.MySQL.getCode().equals(type)) {
            column = "mysql_type";
        } else if (ConnectTypeEnum.Oracle.getCode().equals(type)) {
            column = "oracle_type";
        } else if (ConnectTypeEnum.SQL_Server.getCode().equals(type)) {
            column = "sql_sever_type";
        } else if (ConnectTypeEnum.SAPHANA.getCode().equals(type)) {
            column = "sqphana_type";
        } else if (ConnectTypeEnum.PostgreSQL.getCode().equals(type)) {
            column = "postgresql";
        } else if (ConnectTypeEnum.Kafka.getCode().equals(type)) {
            column = "kafka_type";
        }
        param.eq(column, oriType);
        List<HiveDataType> dataTypes = hiveDataTypeMapper.selectList(param);
        if (CollectionUtils.isNotEmpty(dataTypes)) {
            return dataTypes.get(0).getHiveType();
        }else{
            return "STRING";
        }
    }
}
